package com.sl.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sl.mapper.CompanyCheckMapper;
import com.sl.pojo.CompanyCheck;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Service
public class CompanyCheckService {
    @Resource
    private CompanyCheckMapper companyCheckMapper;

    //查：所有   公司
    public Page<CompanyCheck> findAll(Integer page,Integer pageSize){
        PageHelper.startPage(page,pageSize);
        return (Page<CompanyCheck>) companyCheckMapper.selectAll();
    }
    //查：单个  id   公司
    public CompanyCheck findById(Integer id){
        return companyCheckMapper.selectByPrimaryKey(id);
    }
    public CompanyCheck findByUUId(String uuid){
        Example example = new Example(CompanyCheck.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uuid",uuid);
        return companyCheckMapper.selectOneByExample(example);
    }
    //查：条件   公司
    public Page<CompanyCheck> findList(Map<String,Object> searchMap, Integer page, Integer pageSize){
        PageHelper.startPage(page,pageSize);
        Example example = createExample(searchMap);
        List<CompanyCheck> companies = companyCheckMapper.selectByExample(example);
        return (Page<CompanyCheck>)companies;
    }
    public void deletedCC(CompanyCheck companyCheck){
        companyCheckMapper.delete(companyCheck);
    }



    /**构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(CompanyCheck.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 主键id
            if(searchMap.get("id")!=null ){
                criteria.andEqualTo("id",searchMap.get("id"));
            }
            //唯一标识
            if(searchMap.get("uuid")!=null ){
                criteria.andEqualTo("uuid",searchMap.get("uuid"));
            }
            //公司名称ID
            if(searchMap.get("companyId")!=null ){
                criteria.andEqualTo("companyId",searchMap.get("companyId"));
            }
            //创建时间
            if(searchMap.get("addTime")!=null ){
                criteria.andEqualTo("addTime",searchMap.get("addTime"));
            }
            //检查与否
            if(searchMap.get("checked")!=null ){
                criteria.andEqualTo("checked",searchMap.get("checked"));
            }
            //检测时间
            if(searchMap.get("checkTime")!=null ){
              System.out.println("检查时间>>>>" + searchMap.get("checkTime"));
                criteria.andLike("checkTime","%"+searchMap.get("checkTime")+"%");
            }
            if(searchMap.get("addTime")!=null ){
                criteria.andLike("addTime","%"+searchMap.get("addTime")+"%");
            }
            //检测等级
            if(searchMap.get("level")!=null ){
                criteria.andEqualTo("level",searchMap.get("level"));
            }
            //检查人员
            if(searchMap.get("userIds")!=null ){
                criteria.andLike("userIds", "%"+searchMap.get("userIds")+"%");
            }
            //描述
            if(searchMap.get("description")!=null ){
                criteria.andLike("description", "%"+searchMap.get("description")+"%");
            }
        }
        return example;
    }

    //增：一个
    public void addCompanyCheck(CompanyCheck companyCheck){
        companyCheckMapper.insertSelective(companyCheck);}
    //更新：公司信息
    public void updateCompanyCheck(CompanyCheck companyCheck){
        companyCheckMapper.updateByPrimaryKey(companyCheck);}

    /*public List<CompanyCheck> findMonthList(String str){
        Example example = new Example(CompanyCheck.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("checkTime","%"+"str" +"%");
        return  companyCheckMapper.selectByExample(example);
    }*/

    /**根据日期查询：当月的数据
     * @param firstDay
     * @param lastDay
     * @return
     */
    public List<Map> findListByMonth(LocalDate firstDay,LocalDate lastDay){
        List<Map> listByMonth = companyCheckMapper.findListByMonth(firstDay, lastDay);
        if(listByMonth !=null){
            for (Map usersMap : listByMonth) {
                String[] users = String.valueOf(usersMap.get("users")).split("，");
                usersMap.put("users",users);
            }}
        return listByMonth;
    }
    public Integer passCompanyTotal(LocalDate firstDay,LocalDate lastDay){
        Integer passCompanyTotal = companyCheckMapper.passCompanyTotal(firstDay, lastDay);
        return passCompanyTotal;
    }


}
